package com.esun.rabbit2.viewer.mainact.mylotteryact.bindmobile;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.esun.config.Config;
import com.esun.config.Key;
import com.esun.config.Language;
import com.esun.network.ConnectionChangeReceiver;
import com.esun.rabbit2.communicaters.myinfo.AuthCodeBindMobileCommunicater;
import com.esun.rabbit2.communicaters.myinfo.GetMobileBindAuthCodeCommunicater;
import com.esun.ui.R;
import com.esun.util.ViewerUtil;
import com.esun.util.log.EDebug;

/**
 * @ClassName: BindMobile_unBind_act
 * @Description: 未绑定过手机号Activity
 * @Author: Liujun liuj@500wan.com
 * @Date: 2012-6-20 下午12:02:33
 * 
 */
public class BindMobileUnBindAct extends Activity implements OnClickListener {

	private ImageButton btnGoBack;
	private Button btnJump;
	private Button btnSendAuthCode;// 发送验证码

	private EditText etNum = null;
	private Button btnGetAuthCode;// 得到验证码

	private EditText etAuthCode;

	/**
	 * @Fields strMobileNum :手机号码
	 */
	private String strMobileNum;
	/**
	 * @Fields strAuthoCode : 验证码
	 */
	private String strAuthCode;

	private Handler handler;

	/**
	 * @Fields SEND_AUTHCODE_BIND_SUCCESS : 发送验证码成功
	 */
	private static final int SEND_AUTHCODE_BIND_SUCCESS = 10001;
	/**
	 * @Fields SEND_AUTHCODE_BIND_FAILED : 发送验证码失败
	 */
	private static final int SEND_AUTHCODE_BIND_FAILED = 10002;
	/**
	 * @Fields GET_AUTHCODE_SUCCESS : 获取验证码成功
	 */
	private static final int GET_AUTHCODE_SUCCESS = 10003;
	/**
	 * @Fields GET_AUTHCODE_FAILED : 获取验证码失败
	 */
	private static final int GET_AUTHCODE_FAILED = 10004;

	/**
	 * @Fields current_layout_num : 从哪里跳转过来 注册还是我的彩票
	 */
	private String current_layout_num;
	private String mobileNum = "";
	private ProgressDialog progress;

	private GetMobileBindAuthCodeCommunicater getMobileBindAuthCodecommunicater;
	private AuthCodeBindMobileCommunicater authCodeBindMobileCommunicater;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.bind_mobile_unbind_);
		mobileNum = getIntent().getStringExtra(Key.KEY_MOBILENUM);
		current_layout_num = getIntent().getStringExtra(Key.KEY_STATE);
		EDebug.o("print by liuj ----------------------->current_layout_num=== " + current_layout_num);
		init();
		initHandler();
	}

	/**
	 * @Title: checkInput
	 * @Description: 界面初始化
	 * @Return: void
	 */
	private void init() {

		btnSendAuthCode = (Button) this.findViewById(R.id.bind_mobile_unbind_et_num_btn_send_auth_code);
		btnSendAuthCode.setOnClickListener(this);

		btnGoBack = (ImageButton) this.findViewById(R.id.bind_mobile_unbind_btn_goBack);
		btnGoBack.setOnClickListener(this);

		btnJump = (Button) findViewById(R.id.bind_mobile_unbind_btn_jump);
		btnJump.setOnClickListener(this);

		etAuthCode = (EditText) findViewById(R.id.bind_mobile_unbind_et_num2);
		etAuthCode.setText("");

		etNum = (EditText) this.findViewById(R.id.bind_mobile_unbind_et_num1);
		etNum.setText(mobileNum);

		btnGetAuthCode = (Button) this.findViewById(R.id.bind_mobile_unbind_et_num_btn_get_auth_code);
		btnGetAuthCode.setOnClickListener(this);

		// 如果从注册跳转过来 则显示跳过按钮 隐藏返回按钮
		if (current_layout_num.equals(Config.NUM0)) {
			btnJump.setVisibility(View.VISIBLE);
			btnGoBack.setVisibility(View.GONE);
		} else {
			// 如果从我的彩票跳转过来 则隐藏跳过按钮 显示返回按钮
			btnGoBack.setVisibility(View.VISIBLE);
			btnJump.setVisibility(View.GONE);
		}

		progress = ViewerUtil.showProgressDialog(this, "请稍后...");
	}

	private void initHandler() {
		handler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);

				progress.dismiss();

				switch (msg.what) {
				// 发送验证码成功
				case SEND_AUTHCODE_BIND_SUCCESS:

					ViewerUtil.showTipDialog(BindMobileUnBindAct.this, authCodeBindMobileCommunicater.getServerMsg(), true);

					break;
				// 发送验证码失败
				case SEND_AUTHCODE_BIND_FAILED:
					ViewerUtil.showTipDialog(BindMobileUnBindAct.this, authCodeBindMobileCommunicater.getServerMsg(), false);
					break;
				// 获取验证码成功
				case GET_AUTHCODE_SUCCESS:

					ViewerUtil.showTipDialog(BindMobileUnBindAct.this, getMobileBindAuthCodecommunicater.getServerMsg(), false);
					break;
				// 获取验证码失败
				case GET_AUTHCODE_FAILED:

					ViewerUtil.showTipDialog(BindMobileUnBindAct.this, getMobileBindAuthCodecommunicater.getServerMsg(), false);
					break;
				// 通讯异常
				case Config.CONNECTION_FAIL:

					// ViewerUtil.showTipDialog(BindMobile_unBind_act.this, Language.BINDING_MOBEL_FAIL, false);
					Toast.makeText(BindMobileUnBindAct.this, Language.STR_CONNECTIONFAIL.toString(), Toast.LENGTH_SHORT).show();
					break;

				default:
					break;
				}
			}
		};
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.bind_mobile_unbind_et_num_btn_send_auth_code:

			if (checkInputAuthCode()) {
				progress.show();
				startThread();
			}
			break;
		case R.id.bind_mobile_unbind_et_num_btn_get_auth_code:

			if (checkInput()) {
				progress.show();
				getAuthCodeThread();
			}

			break;

		case R.id.bind_mobile_unbind_btn_goBack:
			this.finish();
			break;

		case R.id.bind_mobile_unbind_btn_jump:
			this.finish();

			break;
		default:
			break;
		}
	}

	/**
	 * @Title: checkInput
	 * @Description: 手机号有效性判断
	 * @Return: boolean 返回类型
	 */
	private boolean checkInput() {

		if (etNum.getText() != null) {
			strMobileNum = etNum.getText().toString();
			if (strMobileNum.length() < 11 || !strMobileNum.substring(0, 1).equals(Config.NUM1)) {
				ViewerUtil.showTipDialog(this, "请输入正确手机号", false);
				return false;
			} else {
				return true;
			}
		} else {
			return false;
		}

	}

	/**
	 * @Title: checkInputAuthCode
	 * @Description: 验证码有效性判断
	 * @Return: boolean 返回类型
	 */
	private boolean checkInputAuthCode() {

		if (etAuthCode.getText() != null) {
			strAuthCode = etAuthCode.getText().toString();
			if (strAuthCode.equals(Config.NULLMARK) || strAuthCode.length() < 1) {
				ViewerUtil.showTipDialog(this, "请输入正确验证码", false);
				return false;
			} else {
				return true;
			}
		} else {
			return false;
		}

	}

	/**
	 * @Title: getAuthCodeThread
	 * @Description: 获取验证码线程
	 * @Return: void 返回类型
	 */
	private void getAuthCodeThread() {
		new Thread() {

			@Override
			public void run() {
				super.run();
				getAuthCodeCommunicater();
			}

		}.start();
	}

	/**
	 * @Title: startThread
	 * @Description: 验证并绑定手机线程
	 * @Return: void 返回类型
	 */
	private void startThread() {
		new Thread() {

			@Override
			public void run() {
				super.run();
				doSubmit();
			}
		}.start();
	}

	/**
	 * @Title: getAuthCodeCommunicater
	 * @Description: 获取验证码方法
	 * @Return: void 返回类型
	 */
	private void getAuthCodeCommunicater() {

		try {
			EDebug.o("print by liuj ----------------------------->------strNum=" + strMobileNum);
			if (getMobileBindAuthCodecommunicater == null) {
				getMobileBindAuthCodecommunicater = new GetMobileBindAuthCodeCommunicater();
			}
			if (getMobileBindAuthCodecommunicater.bind(strMobileNum)) {
				Message msg = handler.obtainMessage(GET_AUTHCODE_SUCCESS);
				handler.sendMessage(msg);
			} else {
				Message msg = handler.obtainMessage(GET_AUTHCODE_FAILED);
				handler.sendMessage(msg);
			}
		} catch (Exception e) {
			Message msg = handler.obtainMessage(Config.CONNECTION_FAIL);
			handler.sendMessage(msg);
		}
	}

	/**
	 * @Title: doSubmit
	 * @Description: 验证验证码并绑定手机
	 * @Return: void
	 */
	private void doSubmit() {

		try {

			EDebug.o("print by liuj ----------------------------->code==" + strAuthCode + "------strNum=" + strMobileNum);
			if (authCodeBindMobileCommunicater == null) {
				authCodeBindMobileCommunicater = new AuthCodeBindMobileCommunicater();
			}
			if (authCodeBindMobileCommunicater.sendCode(strAuthCode, strMobileNum)) {
				Message msg = handler.obtainMessage(SEND_AUTHCODE_BIND_SUCCESS);
				handler.sendMessage(msg);
			} else {
				Message msg = handler.obtainMessage(SEND_AUTHCODE_BIND_FAILED);
				handler.sendMessage(msg);
			}
		} catch (Exception e) {
			Message msg = handler.obtainMessage(Config.CONNECTION_FAIL);
			handler.sendMessage(msg);
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		this.unregisterReceiver(ConnectionChangeReceiver.getConnectionChangeReceiver());
	}

	@Override
	protected void onResume() {
		super.onResume();
		this.registerReceiver(ConnectionChangeReceiver.getConnectionChangeReceiver(), ConnectionChangeReceiver.getIntentFilter());
	}

}
